/**
 * @file Bug告警管理
 * @date 2025/10/27 23:41:38
 * @author lyqjob@yeah.net
 */

import {  useState, useEffect  } from 'react';
import { Table, Space, Modal, Form, message } from 'antd';
import AuthButton from '@/components/AuthButton';
import { adminApi } from "@/services/admin";
import dayjs from '@/utils/dayjs.js';



const BugWarning = () => {
    const [data] = useState([]);
    const [loading, setLoading] = useState(true)
    const [form] = Form.useForm();
    const [open, setOpen] = useState(false);
    const [pageSize, setPageSize] = useState(10)
    const [current, setCurrent] = useState(1)

    // 错误列表列定义
    const columns = [
        {
            title: '错误类型',
            dataIndex: 'type',
            key: '',
        },
        {
            title: '错误信息',
            dataIndex: 'message',
            key: '',
            ellipsis: true,
            width: 300,
        },
        {
            title: '页面URL',
            dataIndex: 'page_url',
            key: '',
            ellipsis: true,
        },
        {
            title: '发生时间',
            dataIndex: 'timestamp',
            key: '',
            render:(text) => dayjs(text).format('YYYY-MM-DD HH:mm:ss')
        },
        {
            title: '会话ID',
            dataIndex: 'session_id',
            key: '',
            ellipsis: true,
        },
    ];


    useEffect(() => {
        getList(current, pageSize)
    }, [])


    const getList = async (_page, _limit) => {
        // const [err, res] = await adminApi.getMonitorlist({ page, limit ,reporttype:'error'});
        // if (err) {
        //     setLoading(false)
        //     // message.warning('获取日志列表失败')
        //     return
        // }
        // if (res?.code === 200 && res?.data?.list) {
        //     setLoading(false)
        //     // setData(res.data.list)
        //     // setTotal(res.data.total)
        //     // window.process.done();
        // }
        setLoading(false)

    }


    const onCreate = async values => {
        const [err, res] = await adminApi.createUser(values)
        if (err) {
            message.warning('添加失败')
            setOpen(false)
        }
        if (res) {
            message.success('添加成功')
            setOpen(false)
            getList( current, pageSize )
        }

    };

    return (
        <div className="container">
            <Space>
                <AuthButton authority="user:add">
                    {/* <Button onClick={() => {setOpen(true) }} type="primary" style={{ marginBottom: 16 }}>
                        添加
                    </Button> */}
                </AuthButton>


            </Space>
            <Table
                loading={loading}
                rowKey={'id'}
                columns={columns}
                rowSelection={false}
                dataSource={data}
                scroll={{
                    x: 1500,
                    y: 450
                }}
                pagination={{
                    pageSize,
                    current,
                    showPage: true,
                    total: 0,
                    showTotal: (total, range) => `${range[0]}-${range[1]} 共 ${total} 条`,
                    onChange: (page, pageSize) => {
                        setPageSize(pageSize)
                        setCurrent(page)
                        getList(page, pageSize)
                    }
                }}
            />

            <Modal
                open={open}
                title="添加用户"
                okText="确定"
                cancelText="取消"
                width={800}
                okButtonProps={{ autoFocus: true, htmlType: 'submit' }}
                onCancel={() => setOpen(false)}
                destroyOnHidden
                modalRender={dom => (
                    <Form
                        labelCol={{ span: 8 }}
                        wrapperCol={{ span: 16 }}
                        layout="horizontal"
                        form={form}
                        name="form_in_modal"
                        initialValues={{ password: '123456', avatar_url: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png' }}
                        clearOnDestroy
                        onFinish={values => onCreate(values)}
                        style={{ maxWidth: 800 }}

                    >
                        {dom}
                    </Form>
                )}
            >


            </Modal>
        </div>
    )
}


export default BugWarning;
